<?php
/**
 * Holds class Image
 *
 * @author daantimmer
 *
 * @deprecated functionality like saving/resizing should be moved to Image_Image but this class is still used though
 *
 * @see Image_Image
 *
 * @package Library
 */

/**
 * Old Image class that represents an image on the HDD
 *
 * @author daantimmer
 *
 */
class Image{

	private $fp;

	private $path;

	private $data;
	
	const AUTO = -1;

	public function __construct($path = null){
		if($path)	$this->load($path);
	}

	public function load($path){
		$this->path = $path;
		$this->close();
		$this->fp = fopen($this->path,"r");
		$this->data = fread($this->fp,filesize($path));
	}

	public function loadURL($url){
		$url = str_replace(" ","%20",$url);
		$this->data = @file_get_contents($url);
	}

	public function save($path){
		$this->path = $path;
		$this->close();
		$this->fp = fopen($this->path,"w");
		fwrite($this->fp,$this->data,strlen($this->data));
	}
	
	public function remove($path){
		unlink($path);
	}
	
	public function saveGD2PNG($path,$name,$rwidth,$rheight,$owidth,$oheight){
		if($this->data === false)
			return false;
		
		$ip = imagecreatefromstring($this->data);
		
		$newr = $this->imageResize($ip,$rwidth,$rheight,true,true);
		$newo = $this->imageResize($ip,$owidth,$oheight,false,false);
		
		imagepng($newr,$path.$name.'.png');
		imagepng($newo,$path.$name.'-original.png');
		
		imagedestroy($ip);
		imagedestroy($newr);
		imagedestroy($newo);
		
		$this->load($path.$name.'-original.png');
	}
	
	public function saveGD2JPG($path,$name,$rwidth,$rheight,$owidth,$oheight){
		if($this->data === false)
			return false;
		
		$ip = imagecreatefromstring($this->data);
		
		$newr = $this->imageResize($ip,$rwidth,$rheight,true,true);
		$newo = $this->imageResize($ip,$owidth,$oheight,false,false);
		
		imagejpeg($newr,$path.$name.'.jpg',100);
		imagejpeg($newo,$path.$name.'-original.jpg',100);
		
		imagedestroy($ip);
		imagedestroy($newr);
		imagedestroy($newo);
		
		$this->load($path.$name.'-original.jpg');
	}
	
	private function imageScale($ip,$maxwidth,$maxheight,$resizetomax = false){
		$ipw = imagesx($ip);
		$iph = imagesy($ip);
		if($resizetomax){
			$xscale = ($maxwidth / $ipw);
			$yscale = ($maxheight / $iph);
		}else{
			$xscale = min($maxwidth / $ipw,1);
			$yscale = min($maxheight / $iph,1);
		}
		return ($xscale < $yscale) ? $xscale : $yscale;
	}
	
	private function imageResize($ip,$maxwidth,$maxheight,$base = false, $resizetomax = false){
		$width = imagesx($ip);
		$height = imagesy($ip);
		$scale = $this->imageScale($ip,$maxwidth,$maxheight,$resizetomax);
		
		$swidth = min(round($scale*$width),$maxwidth);
		$sheight = min(round($scale*$height),$maxheight);
		
		if($base){
			$tip = imagecreatetruecolor($maxwidth,$maxheight);
			$color = imagecolorallocatealpha($tip,0xff,0xff,0xff,127);
			imagefill($tip,0,0,$color);
		}else	$tip = imagecreatetruecolor($swidth,$sheight);
		
		imagecopyresampled($tip,$ip,0,0,0,0,$swidth,$sheight,$width,$height);
		
		return $tip;
	}

	public function close(){
		if($this->fp)	fclose($this->fp);
	}

	public function readPixels($size = 0.8, $step = 5){
		if(!$this->fp)	throw new Exception("no image is opened");

		$ip = imagecreatefromstring($this->data);

		$width = imagesx($ip);
		$height = imagesy($ip);

		$hray = array();

		$sx = round($width/2 - (($width*$size)/2));
		$sy = round($height/2 - (($height*$size)/2));
		
		$width = round($width*$size);
		$height = round($height*$size);
		
		for($y=$sy,$height;$y<$height;$y+=$step){
			for($x=$sx,$width;$x<$width;$x+=$step){
				$index = imagecolorat($ip, $x, $y);
				$rgb = imagecolorsforindex($ip,$index);
					
				$hsv = $this->rgb2hsv($rgb["red"],$rgb["green"],$rgb["blue"]);
				
				$colour = $this->hsv2string($hsv);
				
				if($colour){
					if(isset($hray[$colour])){
						$hray[$colour] ++;
					}else{
						$hray[$colour] =1;
					}
				}
				
//				if($hsl[1] < 2){
//					if(isset($hray["wit"]))
//						$hray["wit"] ++;
//					else
//						$hray["wit"] = 1;
//
//				}elseif($hsl[2] > 25){
//					if(isset($hray[$this->hue2string($hsl[0])]))
//						$hray[$this->hue2string($hsl[0])]++;
//					else
//						$hray[$this->hue2string($hsl[0])] = 1;
//				}else{
//					if(isset($hray["donker"]))
//						$hray["donker"] ++;
//					else
//						$hray["donker"] = 1;
//				}
			}
		}
		$size = ($width * $height) / ($step*$step);
		$hray = array_filter($hray,array(new ColourLimitFilter($size,10), "reduce"));
		asort($hray);
		$hray = array_reverse($hray,true);
		$ray = array();
		
		foreach($hray AS $key => $item){
			$ray[$key] = $item / $size;
		}
		
		
		return $ray;
	}

	function rgb2hsv($r,$g,$b){
		$minVal = min($r, $g, $b);
		$maxVal = max($r, $g, $b);
		$delta  = $maxVal - $minVal;

		$v = $maxVal / 255;

		if ($delta == 0) {
			$h = 0;
			$s = 0;
		} else {
			$s = $delta / $maxVal;
			$del_R = ((($maxVal - $r) / 6) + ($delta / 2)) / $delta;
			$del_G = ((($maxVal - $g) / 6) + ($delta / 2)) / $delta;
			$del_B = ((($maxVal - $b) / 6) + ($delta / 2)) / $delta;

			if ($r == $maxVal){
				$h = $del_B - $del_G;
			} else if ($g == $maxVal) {
				$h = (1 / 3) + $del_R - $del_B;
			} else if ($b == $maxVal) {
				$h = (2 / 3) + $del_G - $del_R;
			}

			if ($h < 0){
				$h++;
			}
			if ($h > 1) {
				$h--;
			}
		}
		$h = round($h * 360);
		$s = round($s * 100);
		$v = round($v * 100);

		return array($h, $s, $v);
	}
	
	public function hue2string($hue){
		if($hue >= 320 || $hue < 30)
			return "Rood";
		if($hue >= 30 && $hue < 80)
			return "Geel";
		if($hue >= 80 && $hue < 160)
			return "Groen";
		if($hue >= 160 && $hue < 260)
			return "Blauw";
		if($hue >= 260 && $hue < 320)
			return "Paars";
		return "undef:{$hue}";
	}
	
	private function hsv2string($hsv){
		$h = $hsv[0];
		$s = $hsv[1];
		$v = $hsv[2];
		
		if($s <= 1 && $v >= 99)	return false; //te wit
		
		if($s <= 3 && $v >= 97)	return "wit";
		
		IF(($h > 30 && $h < 80) && $s > 70 && $v > 40)
								return "geel";
		
		if(($h > 10 && $h < 50) && $s > 40 && ($v > 10 && $v < 90))
								return "bruin";
		
		if(($h > 330 || $h < 30) && ($s > 20 && $s < 60) && ($v > 60))
								return "roze";

		if(($h > 330 || $h < 30) && $s > 60 && $v > 60)
								return "rood";
		
		if(($h > 270 && $h < 330) && $s > 60 && $v > 60)
								return "paars";
								
		if(($h > 170 && $h < 270) && $s > 40 && $v > 40)
								return "blauw";
		
		if(($h > 90 && $h < 140) && $s > 30 && $v > 30)
								return "groen";
								
		if($s < 20 && $v < 20)	return "zwart";
		
		if($s < 10 && ( $v >= 20 && $v < 60))
								return "grijs";
		;
		
		return false;
	}
}

class ColourLimitFilter{
	private $size;
	private $percent;
	
	public function __construct($size,$percent = 25){
		$this->size = $size;
		$this->percent = $percent/100;
	}
	
	public function reduce($var){
		return $var > ($this->size * $this->percent);
	}
}

/**
 *
 *
 * 10 x 12
 * 12 x
 *
 */